<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\VideoLink;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class ShortVideoController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('视频短剧列表');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        $content = new Content();
        $content->header('编辑');
        $content->body($this->form()->edit($id));
        return $content;
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加');
            $content->description('');
            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(VideoLink::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('记录ID');
            $grid->title('短剧标题');
            $grid->introduce('短剧简介');
            $grid->video_cover('封面链接');
            $grid->video_url('视频链接');
            $grid->xcv_url('小程序链接');
            $grid->sort('排序');
            $grid->status('开关')->display(function ($data) {
                return $data == 1 ? '开' : '关';
            });
            $grid->created_at('创建时间');
            $grid->updated_at('更新时间');
        });
    }

    protected function form($mode = '', $id = '')
    {
        return Admin::form(VideoLink::class, function (Form $form) use ($mode, $id) {
            $form->text('title', '短剧标题')->required();
            $form->text('introduce', '短剧简介')->required();
            $form->text('video_cover', '封面链接')->required();
            $form->text('video_url', '视频链接')->required();
            $form->text('xcv_url', '小程序链接')->required();
            $form->select('wx_id','微信选择')->options(VideoLink::getWxId())->default(1);
            $form->hidden('wx_info');
            $form->number('sort', '排序');
            $form->select('status', '开关')->options([
                0 => '关',
                1 => '开'
            ]);
            $form->saving(function (Form $form) {
                $wxId = request('wx_id');
                $link = request('xcv_url') ?? '';
                $form->wx_info = VideoLink::getWxInfo($wxId,$link) ?? '';
            });
        });
    }
}
